package com.wx.wxpush.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.wxpush.entiy.Letter;
import com.wx.wxpush.entiy.Tree;
import com.wx.wxpush.service.TreeService;
import com.wx.wxpush.util.ResultVO;
import com.wx.wxpush.util.TreeUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@Controller
@RequestMapping("/tree")
@Api(tags="tree接口")
public class TreeController {
    @Autowired
    private TreeService treeService;
    @GetMapping("/treeList")
    public ResultVO treeList(){
        List<Tree> treeList=treeService.selectRoundBySql();

        this.getRoleTree(treeList);
        return ResultVO.ok(this.getRoleTree(treeList));

    }
    /**
     *roleBeanList 为查出来的数据
     */
    public List<Tree> getRoleTree(List<Tree> roleBeanList) {
        List<Tree> baseLists = new ArrayList<>();
        // 总菜单，出一级菜单，一级菜单没有父id
        for (Tree e : roleBeanList) {
            if (e.getSuperiorUnit() == null || "".equals(e.getSuperiorUnit())) {
                baseLists.add(e);
            }
        }
        TreeUtil utils = new TreeUtil(baseLists, roleBeanList);
        List<Tree> result = utils.getTree();
        return result;

    }

}
